package com.kakavr.user.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.kakavr.auth.util.EncryptionUtils;
import com.kakavr.base.controller.BaseController;
import com.kakavr.base.exception.ValidateParamException;
import com.kakavr.result.MsgResult;
import com.kakavr.result.constant.MsgConstants;
import com.kakavr.user.service.IUserService;
import com.kakavr.util.SessionUtils;
import com.kakavr.util.ValidatorUtils;
/**
 * 玩家
 * @author gang.yang
 *
 */
@RestController
@RequestMapping("user")
public class UserController extends BaseController {
	Logger logger=Logger.getLogger(UserController.class);
	
	@Resource
	private IUserService userService;

	/**
	 * 完善信息
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "perfect", method = RequestMethod.POST)
	public Map<String, Object> perfectInfo(HttpServletRequest request) {
		String name = request.getParameter("name");
		String phone = request.getParameter("phone");
		Map<String, Object> map = Maps.newHashMap();
		map.put("name", name);
		map.put("phone", phone);
		map.put("id", SessionUtils.getUserId());
		userService.updateUser(map);
		SessionUtils.updateCurUser("name", name);
		SessionUtils.updateCurUser("phone", phone);
		return MsgResult.okResult();
	}

	/**
	 * 修改密码
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "updatePass", method = RequestMethod.POST)
	public Map<String, Object> updatePass(HttpServletRequest request) {
		String password = request.getParameter("password");
		if (!ValidatorUtils.isPass(password)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_2004,
					MsgConstants.MSG_CONTENT_2004, "密码");
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("password", EncryptionUtils.md5(password));
		map.put("id", SessionUtils.getUserId());
		userService.updateUser(map);
		return MsgResult.okResult();
	}

	/**
	 * 获取用户信息
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "userInfo", method = RequestMethod.GET)
	public Map<String, Object> userInfo(HttpServletRequest request) {
		Map<String, Object> map = Maps.newHashMap();
		map.put("id", SessionUtils.getUserId());
		info("-------------------获取用户信息ID："+SessionUtils.getUserId());
		if(null==SessionUtils.getUserId()){
			throw new ValidateParamException(MsgConstants.MSG_CODE_2001,
					MsgConstants.MSG_CONTENT_2001);
		}
		Map<String, Object> user = userService.getUserInfo(map);
		SessionUtils.updateCurUser("times", user.get("times"));
		map.clear();
		map.put("userId", user.get("id"));
		map.put("name", user.get("name"));
		map.put("userName", user.get("username"));
		map.put("times", user.get("times"));
		map.put("headImg", user.get("headImg"));
		Map<String, Object> mapcount=userService.selectCountNum(map);
		if(null!=mapcount){
			map.putAll(mapcount);
		}
		info("++++++获取用户信息："+JSON.toJSONString(user));
		return MsgResult.okResultData(map);
	}
	
	/**
	 * 绑定帐号
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "bindWX", method = RequestMethod.POST)
	public Map<String, Object> bindWX(HttpServletRequest request){
		if(null == SessionUtils.getCurUser().get("wechartId")){
			throw new ValidateParamException(MsgConstants.MSG_CODE_2007,
					MsgConstants.MSG_CONTENT_2007);
		}
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		if (StringUtils.isBlank(userName)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "用户名");
		}
		if (!ValidatorUtils.isPass(password)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_2004,
					MsgConstants.MSG_CONTENT_2004, "密码");
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("username", userName);
		Map<String, Object> user = userService.getUserInfo(map);
		if(null != user && !user.isEmpty()){
			throw new ValidateParamException(MsgConstants.MSG_CODE_1003,
					MsgConstants.MSG_CONTENT_1003, "用户名");
		}
		map.put("password", EncryptionUtils.md5(password));
		map.put("id", SessionUtils.getUserId());
		userService.updateUser(map);
		SessionUtils.updateCurUser("username", userName);
		return MsgResult.okResult();
	}
}
